// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { game } from "../../../../script/game/control/game";
import { edu, workType } from "../../../../script/game/gameType";
import { ListViewAdapter } from "../../../../script/util/ListView/ListViewAdapter";
import { ccs } from "../../../../script/util/services";
import { WinBase } from "../../../../script/wins/WinBase";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends WinBase {

    work: workType = null;
    list = [];
    @property(ListViewAdapter)
    workList: ListViewAdapter = null;
    start () {
        ccs.gf.extendNodeName(this.nodes, this.node.parent);
        //判断有没有工作
        this.showWork();
        this.list = game.playerMgr.heroInfo.works;
        this.setList();
        this.getWorkInfo("qigai");

        this.registerEvent("upWorkInfo",(id) => {
            this.getWorkInfo(id);
        })
        this.registerEvent("levelUp", this.showWork);
        this.touchEndEventScale(this.nodes.hire, () => {
            this.hireWork();
        })
        this.touchEndEventScale(this.nodes.unHire, () => {
            this.unWork();
        })
    }
    showWork() {
        if(game.playerMgr.heroInfo.work) {
            // 
            this.nodes.workList.active = false;
            this.nodes.company.active = true;
            this.setMyWork();
        } else {
            this.nodes.workList.active = true;
            this.nodes.company.active = false;
        }
    }
    setMyWork() {
        this.work = game.playerMgr.heroInfo.works.find(val => game.playerMgr.heroInfo.work.id == val.id);
        this.nodes.curWork.getComponent(cc.Label).string = this.work.levelName[this.work.level];
        this.nodes.curWage.getComponent(cc.Label).string = ccs.gf.getOmitNum(this.work.income);
        this.nodes.curPower.getComponent(cc.Label).string = this.work.power + "";
        this.nodes.curDesc.getComponent(cc.RichText).string = this.work.desc;
    }
    setList() {
        this.workList.updateItems(this.list);
    }
    getWorkInfo(id) {
        if(id) {
            //获取工作的详细信息
            let work = game.playerMgr.getWorkById(id);
            this.work = work;
        }
        this.setInfo();
    }
    setInfo() {
        let work = this.work;
        this.nodes.workName.getComponent(cc.Label).string = `${work.levelName[work.level]}`;
        this.nodes.wage.getComponent(cc.Label).string = `工资：${ccs.gf.getOmitNum(work.income)}`;
        this.nodes.power.getComponent(cc.Label).string = `精力：${work.power}`;
        this.nodes.edu.getComponent(cc.Label).string = `学历：${ccs.dataMgr.getEduById(work.need).name}`;
        this.nodes.desc.getComponent(cc.RichText).string = `简介：${work.desc}`;
    }
    hireWork() {
        if(game.playerMgr.heroInfo.age < 18) {
            return ccs.winMgr.showWin("toast", {msg: "成年以后才可以工作哦"});
        }
        if(this.work.level > 0 && edu[game.playerMgr.heroInfo.edu.id] >= edu[this.work.need] && game.playerMgr.heroInfo.power >= this.work.power) {
            game.recordMgr.addList({name: "你", action: `找了份工作，成为一名${this.work.levelName[this.work.level]}`, effect: `每个月精力减少${this.work.power}`});
            ccs.winMgr.showWin("toast", {msg: "上班啦"});
            game.playerMgr.achieveFinish("work", 1);
            game.playerMgr.work(this.work);
            this.showWork();
        }else { 
            ccs.winMgr.showWin("toast", {msg: "未能通过面试"});
            game.recordMgr.addList({name: "你", action: `去面试，知识技能不够，被拒绝了`});
        }
    }
    unWork() {
        game.recordMgr.addList({name: "你", action: `辞去了${this.work.levelName[this.work.level]}职位`, effect: `精力增加${this.work.power}`});
        game.playerMgr.unWork();
        this.getWorkInfo("qigai");
        this.showWork();
    }
    // update (dt) {}
}
